package Test;

import java.io.File;
import java.io.IOException;

import IndexComponent.*;
import PagedFile.*;
import RecordManagement.*;

public class IndexComponent_Test {
	
	public static void main(String[] args) throws IOException {
		String text = "hello.txt", d_text = "C:\\db_data";
		PF_Manager pfm = new PF_Manager(d_text);
		IX_Manager ixm = new IX_Manager(pfm);
		File f = new File(new File(d_text), text);
		if (f.exists())
			f.delete();
		IX_IndexHandle ifh = ixm.CreateIndex(text, 1, Keyword.KEYTYPESTRING, 10);
		for (int i=1; i<=1000; ++i) {
			ifh.InsertEntry(new StringKeyword("strin"+i, 10), new RID(i, i));
			ifh.InsertEntry(new StringKeyword("strin"+(i+2), 10), new RID(i+2, i+4));
			ifh.ForcePages();
		}
		
		ifh.InsertEntry(new StringKeyword("strin"+0, 10), new RID(0,0));
		ixm.CloseIndex(ifh);
		ifh = ixm.OpenIndex(text, 1);

		IX_IndexScan iscan = ifh.OpenScan(IX_IndexScan.OP_LT, new StringKeyword("strin"+5, 10));
		RID rid = iscan.GetNextEntry();
		while ( rid != null )  {
			System.out.println(rid.pageNum + " " + rid.slotNum);
			rid = iscan.GetNextEntry();
		}
		System.out.println("Yeah!");
	}
	
	public static void int2byte(int n, byte buf[], int offset) {
		buf[offset] = (byte) (n >> 24);
		buf[offset + 1] = (byte) (n >> 16);
		buf[offset + 2] = (byte) (n >> 8);
		buf[offset + 3] = (byte) n;
	}

}
